#ifndef _UTIL_API_H_
#define _UTIL_API_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <libgen.h>
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>
#include <ctype.h>


#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/mount.h>

#define HEAD_LEN 5
#define SYNC_HEADLEN 16

#define FRAME_MAX_LEN 1024*1024
#define READ_TIMEOUT  1000 // 100ms
#define WRITE_TIMEOUT  1000 // 100ms

typedef enum
{
    EVENT_IDLE,
    EVENT_BB_START_TEST,
    EVENT_BB_SEND_DATA,
    EVENT_BB_STOP_TEST,
    EVENT_MAX_NUM
} xdata_type;


typedef struct _PF_BB_SYNC_PKT_t_
{
    unsigned char sync_head[SYNC_HEADLEN];
    unsigned char data[FRAME_MAX_LEN];
}PF_BB_SYNC_PKT_t;

#pragma pack(8)
typedef struct 
{
     unsigned char sync_head[HEAD_LEN];
     unsigned int  data_len;
     unsigned char sync_id;
     xdata_type type;
     int64_t dev;
     unsigned char user;
     unsigned char channel;
     unsigned char is_stream;
     unsigned int  skip_next_frame_len;
     unsigned char is_emit;
     unsigned char* data;
}msg_hdr_t;
#pragma pack()


typedef enum
{
	DEBUG_LEVEL = 0,
	INFO_LEVEL,
	WARN_LEVEL,
	ERR_LEVEL,
	MAX_LOG_LEVEL
};

extern uint32_t u32_log_level_ctrl;

#define PRINT_DEBUG(fmt, args...)  \
		do{\
			if(u32_log_level_ctrl <=DEBUG_LEVEL){\
				printf("[APP DEBUG] [ %s, Line: %d ]  " fmt "\n", __FILE__, __LINE__,  ##args); \
			}\
		}while(0)

#define PRINT_INFO(fmt, args...)  \
		do{\
			if(u32_log_level_ctrl <=INFO_LEVEL){\
				printf("[APP INFO] [ %s, Line: %d ]  " fmt "\n", __FILE__, __LINE__,  ##args); \
			}\
		}while(0)
		
#define PRINT_WARN(fmt, args...)  \
		do{\
			if(u32_log_level_ctrl <=WARN_LEVEL){\
				printf("[APP INFO] [ %s, Line: %d ]  " fmt "\n", __FILE__, __LINE__,  ##args); \
			}\
		}while(0)

#define PRINT_ERR(fmt, args...)  \
		do{\
			if(u32_log_level_ctrl <=ERR_LEVEL){\
				printf("[APP ERR] [ %s, Line:%d ]  " fmt "\n", __FILE__, __LINE__,  ##args); \
			}\
		}while(0)

#define PRINT_TRACE(fmt, args...)  \
        do{\
            printf("[APP TRACE] [%s ,%s, Line:%d ]  " fmt "\n", __func__,__FILE__, __LINE__,	##args); \
        }while(0)

typedef	void *(*ThreadEntryPtrType)(void *);

int MID_UTIL_CreateDetachThread(ThreadEntryPtrType entry, void *para, pthread_t *pid);
void MID_UTIL_Print_Buf(unsigned char* da ,int len);
#endif